<template>
  <div v-show="$store.state.isLoginShow" class="login">
    <div class="bgc"></div>
    <div class="val">
      <div class="top">
        <span>登录</span>
        <span class="cls" @click="clslogin"></span>
      </div>
      <div class="body" v-show="erweima">
        <div class="er-left">
          <img src="@/assets/image/saomadenglu.png" alt="" />
        </div>
        <div class="er-right">
          <h3>扫码登陆</h3>
          <div>
            <img src="@/assets/image/erweima.png" alt="" />
          </div>
          <p>使用 网易云音乐APP 扫码登录</p>
        </div>
      </div>
      <button v-show="erweima" class="btn" @click="switchshow">
        选择其他登录模式
      </button>
      <div class="more" v-show="denglu">
        <div class="left">
          <div class="phone">
            <img src="@/assets/image/pc_phone.png" alt="" />
            <button @click="phoneisLogin">手机号登录</button>
            <button @click="register">注册</button>
          </div>

          <div class="checkbox">
            <input type="checkbox" v-model="agree" @click="agree = !agree" />
            同意《服务条款》《隐私政策》《儿童隐私政策》
          </div>
        </div>
        <div class="right">
          <p><span class="a"></span> 微信登录</p>
          <p><span class="b"></span> QQ登录</p>
          <p><span class="c"></span> 微博登录</p>
          <p><span class="d"></span> 网易邮箱帐号登录</p>
        </div>
        <img
          src="@/assets/image/qiehuanerweima.png"
          class="qiehuaner"
          alt=""
          @click="erweimaView"
        />
      </div>
      <!-- 注册 -->
      <section v-show=" Phonenumshow">
        <input
          type="number"
          placeholder="请输入手机号"
          oninput="if(value.length>11)value=value.slice(0,11)"
          v-model="phoneVal"
        />
        <input type="text" placeholder="用户名" v-model="userName" />
        <input
          type="text"
          placeholder="设置登录密码,不少于8位"
          v-model="passWord"
        />

        <div class="yanzheng">
          <input
            type="number"
            placeholder="请输入验证码"
            oninput="if(value.length>4)value=value.slice(0,4)"
            v-model="yanzhengVal"
          />
          <button @click="nextProve">获取验证码</button>
        </div>
        <button @click="getregister">注册</button>
        <div @click="dengluView">&lt;&nbsp;&nbsp;&nbsp;返回登陆</div>
      </section>
      <!-- 验证码登录 -->
      <div class="PhomeNum" v-show=" MobileCode">
        <input
          type="number"
          placeholder="请输入手机号"
          oninput="if(value.length>11)value=value.slice(0,11)"
          v-model="phoneVal"
        />
        <div class="yanzheng">
          <input
            type="number"
            placeholder="请输入验证码"
            oninput="if(value.length>4)value=value.slice(0,4)"
            v-model="yanzhengVal"
          />
          <button @click="nextProve">获取验证码</button>
        </div>
        <button @click="getregister">登录</button>
      </div>
      <!-- 密码登录 -->
      <div class="PhomeNumlogin" v-show=" passwordshow">
        <input
          type="number"
          placeholder="请输入手机号"
          oninput="if(value.length>11)value=value.slice(0,11)"
          v-model="phoneVal"
        />
        <input type="text" placeholder="请输入密码" v-model="passWord" />
        <button @click="gophonelogin">登录</button>
      </div>
      <transition name="fade">
        <Tishi></Tishi>
      </transition>
    </div>
  </div>
</template>
<script>
import Tishi from "@/components/tishi.vue";
import {
  SendCodeApi,
  YzCodeApi,
  phoneisloginApi,
  useaNamerepeatApi,
  goPasswordApi,
} from "@/request/api";
export default {
  data() {
    return {
      // 二维码界面
      erweima: true,
      // 登录界面
      denglu: false,
      // 条例阅读
      agree: false,
      // 注册界面手机密码
      Phonenumshow: false,
      // 验证码登录
      MobileCode: false,
      // 密码登录
      passwordshow: false,
      // 手机号，验证码，密码
      phoneVal: 18684074306,
      passWord: "",
      yanzhengVal: "",
      userName: "小小明",
    };
  },
  methods: {
    // 关闭登录框
    clslogin(){
     this.$store.state.isLoginShow = false
    },
    // 判断是否为电话号码
    panduan() {
      let s =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      return s.test(this.PhoneVal);
    },
    // 切到更多登录方式
    erweimaView() {
      this.erweima = true;
      this.denglu = false;
      this.agree = false;
    },
    // 返回二维码登录
    switchshow() {
      this.erweima = false;
      this.denglu = true;
    },
    // 进入注册
    register() {
      if (!this.agree) {
        this.$store.dispatch(
          "activetionTishi",
          "请先勾选同意《服务条款》、《隐私政策》、《儿童隐私政策》"
        );
        return;
      }
      this.denglu = false;
      this.Phonenumshow = true;
    },
    // 返回更多登录
    dengluView() {
      this.denglu = true;
      this.agree = false;
      this.Phonenumshow = false;
    },
    // 发送验证码
    nextProve() {
      if (!this.panduan()) {
        SendCodeApi({ phone: this.phoneVal }).then((res) => {
          this.$store.dispatch("activetionTishi", "验证码发送成功，注意查看");
        });
      }
    },
    // 注册
    getregister() {
      // 检测手机号码是否已注册
      phoneisloginApi({ phone: this.phoneVal }).then((res) => {
        if (res.data.hasPassword) {
          this.$store.dispatch("activetionTishi", "此号码被注册");
          return;
        }
        // 验证验证码
        YzCodeApi({
          phone: this.phoneVal,
          captcha: this.yanzhengVal,
        }).then((res) => {
          if (!res.data.data) {
            this.$store.dispatch("activetionTishi", "验证码错误");
            return;
          }
          // 是否从名
          useaNamerepeatApi({ nickname: this.userName }).then((res) => {
            if (res.data.duplicated) {
              console.log(res.data.duplicated);
              this.$store.dispatch("activetionTishi", "该用户名已经被注册");
              return;
            }
            this.denglu = true;
            this.false = false;
          });
        });
      });
    },
    // 手机号码登录
    phoneisLogin() {
      if (!this.agree) {
        this.$store.dispatch(
          "activetionTishi",
          "请先勾选同意《服务条款》、《隐私政策》、《儿童隐私政策》"
        );

        return;
      }
      this.denglu = false;
      this.passwordshow = true;
    },
    // 密码登录
    gophonelogin() {
      goPasswordApi(this.phoneVal, this.passWord).then(
        (res) => {
          console.log(res);
          if (!res.data) {
            this.$store.dispatch("activetionTishi", "密码错误");
            return;
          }
          this.$store.state.isLoginShow = false;
        }
      );
    },
  },
  components: {
    Tishi,
  },
};
</script>
 
<style lang = "less" scoped>
.login {
  z-index: 999;
  position: fixed;
  top: 0;
  height: 100%;
  width: 100%;
}
.bgc {
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 0, 0.4);
}
.val {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  height: 370px;
  width: 538px;

  .top {
    height: 36px;
    background-color: #333;
    display: flex;
    justify-content: space-between;
    font-weight: bold;
    font-size: 14px;
    border-radius: 4px 4px 0 0;
    padding: 0 20px 0 18px;
    line-height: 38px;
  }
  .cls {
    cursor: pointer;
    margin-top: 8px;
    height: 15px;
    width: 15px;
    background: url(../assets/image/icon.png);
    background-position: 15px -362px;
  }
  .body {
    border-radius: 3px;
    width: 538px;
    height: 326px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    padding-top: 30px;
    .er-left {
      box-sizing: content-box;
      width: 125px;
      height: 220px;
      img {
        width: 100%;
        width: 100%;
      }
    }
    .er-right {
      font-size: 12px;
      color: #333;
      margin-left: 25px;
      width: 200px;
      text-align: center;
      h3 {
        line-height: 30px;
        font-size: 18px;
        font-weight: 500;
      }
      img {
        margin: 20px 0;
      }
      p {
        color: rgba(0, 0, 0, 0.4);
        font-size: 12px;
        line-height: 17px;
        text-align: center;
      }
    }
  }
  .more {
    color: black;
    width: 538px;
    height: 326px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    padding-top: 30px;
    .qiehuaner {
      position: absolute;
      right: 0;
      bottom: -22px;
      width: 52px;
      height: 52px;
    }
    .phone {
      width: 224px;
      padding: 0 35px 3px 40px;
      border-right: 1px dotted #ccc;
      img {
        width: 224px;
        height: 120px;
      }
      button {
        height: 31px;
        padding: 0 15px 0 20px;
        width: 219px;
        margin-top: 10px;
        background-color: #fff;
        border: 1px solid #000;
        border-radius: 3px;
        &:hover {
          background-color: #4e97db;
        }
      }
    }
    .checkbox {
      margin-top: 50px;
    }
    .right {
      padding: 3px 0 3px 39px;
      margin-top: -15px;
      p {
        text-align: left;
        margin-top: 15px;
        height: 40;
        line-height: 40px;
        span {
          margin-right: 5px;
          display: inline-block;
          height: 20px;
          width: 20px;
          background-color: #000;
          border-radius: 50%;
          background: url(@/assets/image/logins.png);
          transform: scale(2);
        }
        .a {
          background-position: 45px 2px;
        }
        .b {
          background-position: 19px 0px;
        }
        .c {
          background-position: 45px 2px;
        }
        .d {
          background-position: 102px 2px;
        }
      }
    }
  }
}
.btn {
  position: relative;
  bottom: 90px;
  display: block;
  width: 118px;
  height: 28px;
  margin: 20px auto 0;
  padding-right: 0;
  font-size: 12px;
  border: 1px solid #979797;
  border-radius: 15px;
  line-height: 28px;
  text-align: center;
  color: rgba(0, 0, 0, 0.8);
  background-color: #fff;
}
section {
  background-color: #fff;
  padding-top: 36px;
  color: #000;
  input,
  button {
    height: 30px;
    width: 220px;
    display: block;
    margin: 0 auto;
    border-radius: 5px;
    outline: none;
    text-indent: 1em;
    border: 1px solid #000;
  }
  :nth-child(1) {
    margin-bottom: 12px;
  }
  :nth-child(2) {
    margin-bottom: 12px;
  }
  :nth-child(3) {
    margin-bottom: 12px;
  }
  :nth-child(5) {
    margin-top: 30px;
    background-color: rgb(12, 114, 195);
    border: none;
    color: 3fff;
  }
  :nth-child(6) {
    cursor: pointer;
    margin-top: 60px;

    background-color: #f7f7f7;
    height: 40px;
    line-height: 40px;
    text-align: left;
    color: rgb(12, 114, 195);
    padding-left: 20px;
    border-top: 1px solid #c6c6c6;
  }
  .yanzheng {
    margin-top: 10px;
    height: 30px;
    width: 220px;
    margin: 0 auto;
    border-radius: 5px;
    input {
      float: left;
      text-indent: 1em;
      outline: none;
      width: 130px;
      height: 100%;
    }
    button {
      width: 80px;
      float: right;
      height: 100%;
      background-color: rgb(12, 114, 195);
      color: #fff;
      padding: 0;
      text-align: 0em;
    }
  }
}

.PhomeNum {
  height: 200px;
  background-color: #fff;
  padding-top: 36px;
  color: #000;
  input,
  button {
    height: 30px;
    width: 220px;
    display: block;
    margin: 0 auto;
    border-radius: 5px;
    outline: none;
    text-indent: 1em;
    border: 1px solid #000;
  }
  :nth-child(1) {
    margin-bottom: 12px;
  }
  :nth-child(2) {
    margin-bottom: 12px;
  }
  :nth-child(3) {
    transform: translateX(-146px);
  }
  .yanzheng {
    margin-top: 10px;
    height: 30px;
    width: 220px;
    margin: 0 auto;
    border-radius: 5px;
    input {
      float: left;
      text-indent: 1em;
      outline: none;
      width: 130px;
      height: 100%;
    }
    button {
      width: 80px;
      float: right;
      height: 100%;
      background-color: rgb(12, 114, 195);
      color: #fff;
      padding: 0;
      text-align: 0em;
    }
  }
}

.PhomeNumlogin {
  height: 200px;
  background-color: #fff;
  padding-top: 36px;
  color: #000;
  input,
  button {
    height: 30px;
    width: 220px;
    display: block;
    margin: 0 auto;
    border-radius: 5px;
    outline: none;
    text-indent: 1em;
    border: 1px solid #000;
  }
  :nth-child(1) {
    margin-bottom: 12px;
  }
  :nth-child(2) {
    margin-bottom: 12px;
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 1s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>